package com.groupseven.happycampus.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.groupseven.happycampus.common.bo.BicycleBo;
import com.groupseven.happycampus.common.instener.WebBicycleListener;
import com.groupseven.happycampus.common.result.R;
import com.groupseven.happycampus.common.pojo.Bicycle;
import com.groupseven.happycampus.common.vo.BicycleBrandPieVo;
import com.groupseven.happycampus.common.vo.BicycleColorBarVo;
import com.groupseven.happycampus.common.vo.LayuiPageVo;
import com.groupseven.happycampus.service.BicycleService;
import com.groupseven.happycampus.common.utils.OssUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/bicycle")
public class BicycleController {

    @Autowired
    private BicycleService bicycleService;

    @RequestMapping("/bicycleList")
    public LayuiPageVo<Bicycle> bicycleList(@RequestParam Map<String, Object> params) {
        System.out.println(params);
        return bicycleService.bicycleListPageData(params);
    }


    @RequestMapping("/bicycleAdd")
    public R bicycleAdd(Bicycle bicycle) {
        System.out.println("******" + bicycle);
        int i = bicycleService.bicycleAdd(bicycle);
        if (i == 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @RequestMapping("/bicycleUpdate")
    public R bicycleUpdate(Bicycle bicycle) {
        System.out.println(bicycle);
        int i = bicycleService.bicycleUpdate(bicycle);
        if (i == 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @RequestMapping("/bicycleDelete")
    public R bicycleDelete(Integer id) {
        System.out.println("传进来的id" + id);
        int i = bicycleService.bicycleDelete(id);
        System.out.println("最终返回i的值" + i);
        if (i == 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }


    @RequestMapping("/deleteBicycleByIds")
    public R deleteBicycleByIds(@RequestParam String ids){
        System.out.println(ids);
        int i = bicycleService.deleteBicycleByIds(ids);
        if (i > 0) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @RequestMapping("/upload")
    public R upload(MultipartFile file) throws IOException {
        if (file != null) {
            //得到文件名
            String originalFilename = file.getOriginalFilename();
            // 将文件转为输入流
            InputStream inputStream = file.getInputStream();
            //把文件 url 写给前端
            String url = OssUtils.uploadFile(inputStream, originalFilename);
            return R.ok().data("url", url);
        }
        return R.ok();
    }


    @RequestMapping("/echartBar")
    public R echartBar(){
        List<BicycleColorBarVo> list = bicycleService.colorBarData();
        System.out.println("list======"+list);
        return R.ok().data("colorBar",list);
    }

    @RequestMapping("/echartsPie")
    public R echartsPie(){
        List<BicycleBrandPieVo> list = bicycleService.brandPieData();
        return R.ok().data("brandPie",list);
    }




    @Autowired
    private WebBicycleListener webBicycleListener;


    @RequestMapping("/export")
    public void export(Bicycle bicycle, HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String filename = URLEncoder.encode("自行车信息导出", "utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + filename + ".xlsx");
        ServletOutputStream outputStream = response.getOutputStream();
        ExcelWriterBuilder write = EasyExcel.write(outputStream, Bicycle.class);
        ExcelWriterSheetBuilder sheet = write.sheet();
        List<BicycleBo> bicycleBoList = bicycleService.bicycleExcelData(bicycle);
        sheet.doWrite(bicycleBoList);
    }


    @RequestMapping("/import")
    public R importExcel(MultipartFile file) throws IOException {
        if (file != null) {
        /**
         * 参数1  文件流
         * 参数2  读取到的数据封装到哪个数据类型中
         * 参数3  监听器  每读取一行数据 就会调用监听器中的 invoke  方法
         * */
            ExcelReaderBuilder read = EasyExcel.read(file.getInputStream(), BicycleBo.class, webBicycleListener);
            // 得到 sheet 对象
            ExcelReaderSheetBuilder sheet = read.sheet();
            // 读取sheet
            sheet.doRead();
        }
        return R.ok();
    }

}
